package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.impl.VodServiceImpl;
import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	private final TrackInfoService trackInfoService;
	private final VodServiceImpl vodService;
	@Autowired
	public TrackInfoApiController(TrackInfoService trackInfoService, VodServiceImpl vodService) {
		this.trackInfoService = trackInfoService;
		this.vodService = vodService;
	}

	/**
	 * @description: 上传声音
	 * @author Petrichor
	 * @date 2023/12/22 16:42
	 * @param file 文件数据
	 * @return uploadTrackVo 给前端返回的媒体文件信息 包含[媒体文件的id,媒体文件的url]
	**/
	@PostMapping("/uploadTrack")
	public Result uploadTrack(@RequestBody MultipartFile file){
		UploadTrackVo uploadTrackVo = vodService.uploadTrack(file);
		return Result.build(uploadTrackVo, 200, uploadTrackVo.getCoverUrl());
	}
	/**
	 * @description: 新增声音
	 * @author Petrichor
	 * @date 2023/12/22 18:33
	 * @param trackInfoVo  声音信息
	 * @return NULL
	**/
	@GuiGuLogin
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}
	/**
	 * @description: 获取当前用户声音分页列表
	 * @author Petrichor
	 * @date 2023/12/22 19:48
	 * @param page 当前页面
	 * @param limit 每页记录数
	 * @return 当前用户声音分页列表
	**/
	@GuiGuLogin
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackListVo>> findUserTrackPage(@Parameter(name = "page",description = "当前页面",required = true)
														@PathVariable Long page,
														@Parameter(name = "limit",description = "每页记录数",required = true)
														@PathVariable Long limit,
														@Parameter(name = "trackInfoQuery",description = "查询对象",required = false)
														@RequestBody TrackInfoQuery trackInfoQuery){
		//	设置当前用户Id
		trackInfoQuery.setUserId(AuthContextHolder.getUserId());
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(page , limit ,trackInfoQuery);
		//	返回数据
		return Result.ok(trackListVoIPage);
	}
	/**
	 * @description: 根据id查询声音信息
	 * @author Petrichor
	 * @date 2023/12/22 21:03
	 * @param  id 专辑id
	 * @return TrackInfo 声音信息
	**/
	@Operation(summary = "根据id查询声音信息")
	@GetMapping("getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}
	/**
	 * @description: 修改声音
	 * @author Petrichor
	 * @date 2023/12/22 21:09
	 * @param  id 专辑id
	 * @param  trackInfoVo 声音信息
	**/
	@Operation(summary = "修改声音")
	@PutMapping("updateTrackInfo/{id}")
	public Result updateById(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}
	/**
	 * @description: 删除声音信息
	 * @author Petrichor
	 * @date 2023/12/22 21:26
	 * @param id 专辑id
	**/
	@Operation(summary = "删除声音信息")
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}
	/**
	 * @description: 根据专辑id获取声音列表
	 * @author Petrichor
	 * @date 2024/1/1 14:25
	 * @param albumId 专辑id
	 * @param pageNo 页数
	 * @param limit 当前页的条数
	 * @return AlbumTrackListVo 用户专辑声音列表信息
	**/
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping(value = "/findAlbumTrackPage/{albumId}/{page}/{limit}")
	@GuiGuLogin(required = false)
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId ,
															  @PathVariable(value = "page") Long pageNo ,
															  @PathVariable(value = "limit") Long limit) {
		IPage<AlbumTrackListVo> albumTrackListVoIPage = trackInfoService.findAlbumTrackPage(albumId , pageNo , limit) ;
		return Result.build(albumTrackListVoIPage , ResultCodeEnum.SUCCESS) ;
	}
	/**
	 * @description: 统计声音信息接口
	 * @author Petrichor
	 * @date 2024/1/1 16:42
	 * @param trackId 声音id
	 * @return TrackStatVo 用户声音统计信息
	**/
	@Operation(summary = "获取声音统计信息")
	@GetMapping("getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVoByTrackId(trackId);
		return Result.ok(trackStatVo);
	}
	/**
	 * @description: 获取用户声音分集购买支付列表
	 * @author Petrichor
	 * @date 2024/1/7 21:46
	 * @param trackId 声音id
	 * @return TrackPayVo 声音分集购买信息列表
	**/
	@GuiGuLogin
	@Operation(summary = "获取用户声音分集购买支付列表")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result<List<TrackPayVo>> findUserTrackPaidList(@PathVariable Long trackId) {
		List<TrackPayVo> trackPayVoList = trackInfoService.findUserTrackPaidList(trackId);
		return Result.ok(trackPayVoList);
	}
}

